TYPE
  LOGLEVEL : (CRITICAL, WARNING, INFO, DEBUG) := INFO;
END_TYPE

FUNCTION_BLOCK LOGGER
  VAR_INPUT
    TRIG : BOOL;
    MSG : STRING;
    LEVEL : LOGLEVEL := INFO;
  END_VAR
  VAR
    TRIG0 : BOOL;
  END_VAR

  IF TRIG AND NOT TRIG0 THEN
  {{
   LogMessage(GetFbVar(LEVEL),(char*)GetFbVar(MSG, .body),GetFbVar(MSG, .len));
  }}
  END_IF;
  TRIG0:=TRIG;
END_FUNCTION_BLOCK



FUNCTION_BLOCK proc_state
  VAR_INPUT
    ERR_HOME : BOOL;
    ERR_HOME_ID : WORD;
    ERR_MOVE : BOOL;
    ERR_MOVE_ID : WORD;
    ERR_MOVE1 : BOOL;
    ERR_MOVE2 : BOOL;
    HOME : BOOL;
    ACT_MOVE : BOOL;
    ACT_MOVE0 : BOOL;
  END_VAR
  VAR_OUTPUT
    OUT : INT;
  END_VAR

  OUT:=0;

  if HOME then
  OUT:=1;
  end_if;

  if ACT_MOVE then
  OUT:=2;
  end_if;

  if ERR_MOVE then
  OUT:=-WORD_TO_INT(ERR_MOVE_ID);
  end_if;

  if ERR_HOME then
  OUT:=-WORD_TO_INT(ERR_HOME_ID);
  end_if;
END_FUNCTION_BLOCK

FUNCTION_BLOCK stepper
  VAR_INPUT
    AXIS : AXIS_REF;
  END_VAR
  VAR_OUTPUT
    POS : REAL;
    STATE : INT;
  END_VAR
  VAR_INPUT
    TARGET : REAL;
  END_VAR
  VAR_IN_OUT
    RUN : BOOL;
  END_VAR
  VAR_OUTPUT
    RUNING : BOOL;
  END_VAR
  VAR_INPUT
    SPEED : REAL;
    ASPEED : REAL;
  END_VAR
  VAR_IN_OUT
    HOME : BOOL;
  END_VAR
  VAR_INPUT
    HOMESPEED : REAL;
    HOMELOOKUP : REAL;
    XIFEN : REAL;
    RUN_DIR : MC_DIRECTION;
    HOMEMODE : MC_HOMING_PROCEDURES;
  END_VAR
  VAR
    BUFFMODE : MC_BUFFER_MODE := mcAborting;
    MC_SetPosMODE : MC_EXECUTION_MODE := mcImmediately;
    MC_WriteParameter8 : MC_WriteParameter;
    MC_Home1 : MC_Home;
    MC_MoveAbsolute3 : MC_MoveAbsolute;
    MC_Power3 : MC_Power;
    MC_ReadActualPosition3 : MC_ReadActualPosition;
    proc_state3 : proc_state;
    MC_SetPosition3 : MC_SetPosition;
    MC_WriteParameter0 : MC_WriteParameter;
    MC_WriteParameter6 : MC_WriteParameter;
    R_TRIG0 : R_TRIG;
    _TMP_MOVE138_ENO : BOOL;
    _TMP_MOVE138_OUT : BOOL;
    _TMP_OR222_OUT : BOOL;
    _TMP_DIV167_OUT : REAL;
  END_VAR

  MC_ReadActualPosition3(Axis := AXIS, Enable := MC_Power3.Status);
  POS := MC_ReadActualPosition3.Position;
  _TMP_MOVE138_OUT := MOVE(EN := MC_MoveAbsolute3.Done, IN := 0, ENO => _TMP_MOVE138_ENO);
  IF _TMP_MOVE138_ENO THEN
      RUN := _TMP_MOVE138_OUT;
  END_IF;
  proc_state3(ERR_HOME := MC_Home1.Error, ERR_HOME_ID := MC_Home1.ErrorID, ERR_MOVE := MC_MoveAbsolute3.Error, HOME := MC_Home1.Active, ACT_MOVE := MC_MoveAbsolute3.Active);
  STATE := proc_state3.OUT;
  _TMP_OR222_OUT := OR(MC_MoveAbsolute3.Busy, MC_Home1.Busy);
  RUNING := _TMP_OR222_OUT;
  MC_Power3(Axis := AXIS, Enable := 1, EnablePositive := 1, EnableNegative := 1);
  MC_Home1(Axis := AXIS, Execute := MC_WriteParameter0.Done, HomingMode := HOMEMODE, BufferMode := BUFFMODE);
  MC_SetPosition3(Axis := AXIS, Execute := MC_Home1.Done, Position := 0.0, ExecutionMode := MC_SetPosMODE);
  MC_MoveAbsolute3(Axis := AXIS, Execute := R_TRIG0.Q, ContinuousUpdate := 1, Position := TARGET, Velocity := SPEED, Acceleration := _TMP_DIV167_OUT, Deceleration := _TMP_DIV167_OUT, Direction := RUN_DIR);
  R_TRIG0(CLK := RUN);
  _TMP_DIV167_OUT := DIV(SPEED, ASPEED);
  MC_WriteParameter8(Axis := AXIS, Execute := HOME, ParameterNumber := 1018, Value := HOMELOOKUP);
  MC_WriteParameter0(Axis := AXIS, Execute := MC_WriteParameter8.Done, ParameterNumber := 1010, Value := HOMESPEED);
  MC_WriteParameter6(Axis := AXIS, Execute := MC_Power3.Status, ParameterNumber := 1003, Value := XIFEN);
END_FUNCTION_BLOCK

PROGRAM program0
  VAR
    M1_SPEED : REAL := 100.0;
    M1_POS : REAL;
    M1_POS_SET : REAL;
    M1_ASPEED : REAL := 1.0;
    M1_RUN : BOOL;
    M1_STATUS : INT;
    HOME1 : BOOL;
    HOME2 : BOOL;
    HOME3 : BOOL;
    HOME6 : BOOL;
    HOME7 : BOOL;
  END_VAR
  VAR RETAIN
    HOME_SPEED1 : REAL := 50.0;
    HOME_SPEED2 : REAL := 50.0;
    HOME_SPEED3 : REAL := 50.0;
    HOME_SPEED6 : REAL := 50.0;
    HOME_SPEED7 : REAL := 2.0;
    HOME_SPEED_BACK1 : REAL := 10.0;
    HOME_SPEED_BACK2 : REAL := 10.0;
    HOME_SPEED_BACK3 : REAL := 5.0;
    HOME_SPEED_BACK6 : REAL := 10.0;
    HOME_SPEED_BACK7 : REAL := 6.0;
    XIFEN1 : REAL := 1066.0;
    XIFEN2 : REAL := 355.0;
    XIFEN3 : REAL := 1066.0;
    XIFEN6 : REAL := 160.0;
    XIFEN7 : REAL := 4266.0;
    M2_SPEED : REAL := 100.0;
  END_VAR
  VAR
    M2_POS : REAL;
    M2_POS_SET : REAL;
  END_VAR
  VAR RETAIN
    M2_ASPEED : REAL := 1.0;
  END_VAR
  VAR
    M2_STATUS : INT;
    M2_RUN : BOOL;
  END_VAR
  VAR RETAIN
    M3_SPEED : REAL := 100.0;
  END_VAR
  VAR
    M3_POS : REAL;
    M3_POS_SET : REAL;
  END_VAR
  VAR RETAIN
    M3_ASPEED : REAL := 1.0;
  END_VAR
  VAR
    M3_STATUS : INT;
    M3_RUN : BOOL;
  END_VAR
  VAR RETAIN
    M3_Delay : REAL := 0.1;
  END_VAR
  VAR
    M4_FORCE : BYTE;
    M4_ACT : BOOL := 1;
    M4_POS : BYTE;
    M4_SPEED : BYTE;
    M4_RUN : BYTE;
    M4_MODE : BYTE;
    M4_STATUS : WORD;
  END_VAR
  VAR
    M5_SPEED AT %IW4.0.0.20 : WORD;
    M5_POS AT %IW4.0.0.33 : WORD;
  END_VAR
  VAR RETAIN
    M5_SPEED_SET AT %QW4.0.2.20 : WORD := 500;
  END_VAR
  VAR
    M5_POS_SET AT %QW4.0.1.32 : WORD;
    M5_STATUS AT %IW4.0.0.36 : WORD;
  END_VAR
  VAR RETAIN
    M6_SPEED : REAL := 100.0;
  END_VAR
  VAR
    M6_POS : REAL;
    M6_POS_SET : REAL;
  END_VAR
  VAR RETAIN
    M6_ASPEED : REAL := 1.0;
  END_VAR
  VAR
    M6_STATUS : INT;
    M6_RUN : BOOL;
  END_VAR
  VAR RETAIN
    M7_SPEED : REAL := 6.0;
  END_VAR
  VAR
    M7_POS : REAL;
    M7_POS_SET : REAL;
  END_VAR
  VAR RETAIN
    M7_ASPEED : REAL := 0.1;
  END_VAR
  VAR
    M7_STATUS : INT;
    M7_RUN : BOOL;
    M8_POS : BYTE;
  END_VAR
  VAR RETAIN
    M8_SPEED : BYTE := 127;
    M8_FORCE : BYTE := 255;
    M8_ACT : BOOL := 1;
    M8_MODE : BYTE := 1;
  END_VAR
  VAR
    M8_RUN : BYTE;
    M8_STATUS : WORD;
  END_VAR
  VAR RETAIN
    M9_ACT : BOOL := 1;
    M9_SPEED : BYTE := 88;
    M9_FORCE : BYTE := 255;
  END_VAR
  VAR
    M9_RUN : BYTE;
    M9_POS : INT := 0;
    M9_STATUS : WORD;
    liandong : BOOL;
    liandong_POS : REAL;
  END_VAR
  VAR RETAIN
    M9_SCALE : REAL := 90.0;
    M7_DELAY : REAL := 0.48;
  END_VAR
  VAR
    X1 AT %IX1.1.1 : BOOL;
    X2 AT %IX1.1.2 : BOOL;
    X3 AT %IX1.1.3 : BOOL;
    X4 AT %IX1.1.4 : BOOL;
    X5 AT %IX1.1.5 : BOOL;
    X6 AT %IX1.1.6 : BOOL;
    MB3_HR_1000 AT %MW3.0.0.1000 : WORD;
    MB3_HR_1001 AT %MW3.0.0.1001 : WORD;
    MB3_HR_1002 AT %MW3.0.0.1002 : WORD;
    MB3_HR_1003 AT %MW3.0.0.1003 : WORD;
    MB3_HR_2000 AT %IW3.0.1.2000 : WORD;
    MB_HR_2000 AT %IW2.1.0.2000 : WORD;
    MB_HR_2001 AT %IW2.1.0.2001 : WORD;
    MB_HR_2002 AT %IW2.1.0.2002 : WORD;
    MB_HR_2003 AT %IW2.1.0.2003 : WORD;
    MB_HR_2004 AT %IW2.1.0.2004 : WORD;
    MB_HR_2005 AT %IW2.1.0.2005 : WORD;
    MB_HR_2006 AT %IW2.1.0.2006 : WORD;
    MB_HR_2007 AT %IW2.1.0.2007 : WORD;
    MB_HR_2008 AT %IW2.1.0.2008 : WORD;
    MB_HR_1000 AT %MW2.1.2.1000 : WORD;
    MB_HR_1001 AT %MW2.1.2.1001 : WORD;
    MB_HR_1002 AT %MW2.1.1.1002 : WORD;
    MB_HR_1003 AT %MW2.1.1.1003 : WORD;
    MB_HR_1004 AT %MW2.1.1.1004 : WORD;
    MB_HR_1005 AT %MW2.1.1.1005 : WORD;
    MB_HR_1006 AT %MW2.1.1.1006 : WORD;
    MB_HR_1007 AT %MW2.1.1.1007 : WORD;
    MB_2_1_0_STATUS AT %IW2.1.0.0.7 : INT;
    MB_2_1_0_COUNT AT %ID2.1.0.0.8 : UDINT;
    MB_2_1_0_COUNT0 AT %ID2.1.0.0.9 : UDINT;
    MB_3_0_1_STATUS AT %IW3.0.1.0.7 : INT;
    MB_3_0_1_COUNT AT %ID3.0.1.0.8 : UDINT;
    MB_3_0_1_COUNT0 AT %ID3.0.1.0.9 : UDINT;
    MB_4_0_0_STATUS AT %IW4.0.0.0.7 : INT;
    MB_4_0_0_COUNT AT %ID4.0.0.0.8 : UDINT;
    MB_4_0_0_COUNT0 AT %ID4.0.0.0.9 : UDINT;
  END_VAR
  VAR
    stepper0 : stepper;
    AXIS1 : AXIS_REF := 0;
    AXIS2 : AXIS_REF := 1;
    AXIS3 : AXIS_REF := 2;
    AXIS4 : AXIS_REF := 3;
    AXIS5 : AXIS_REF := 4;
    AXIS6 : AXIS_REF := 5;
    stepper1 : stepper;
    stepper2 : stepper;
    stepper3 : stepper;
    stepper4 : stepper;
  END_VAR
  VAR
    Y1 AT %QX1.2.1 : BOOL;
    Y2 AT %QX1.2.2 : BOOL;
  END_VAR
  VAR
    HOME_P : MC_HOMING_PROCEDURES := mcHomeAbsoluteSwitchPositive;
    HOME_N : MC_HOMING_PROCEDURES := mcHomeAbsoluteSwitchNegative;
    DIRC_P : MC_DIRECTION := mcPositiveDirection;
    DIRC_N : MC_DIRECTION := mcNegativeDirection;
    TON0 : TON;
    TON1 : TON;
    R_TRIG0 : R_TRIG;
    RS0 : RS;
    TON2 : TON;
    F_TRIG0 : F_TRIG;
    R_TRIG1 : R_TRIG;
    R_TRIG2 : R_TRIG;
    TON1_Q : BOOL;
    TON2_Q : BOOL;
    _TMP_ADD165_ENO : BOOL;
    _TMP_ADD165_OUT : INT;
    _TMP_ADD126_ENO : BOOL;
    _TMP_ADD126_OUT : REAL;
    _TMP_MOVE128_ENO : BOOL;
    _TMP_MOVE128_OUT : BYTE;
    _TMP_MOVE168_ENO : BOOL;
    _TMP_MOVE168_OUT : BOOL;
    _TMP_BYTE_2_WORD43_OUT : WORD;
    _TMP_BYTE_2_WORD47_OUT : WORD;
    _TMP_BYTE_2_WORD67_OUT : WORD;
    _TMP_BYTE_2_WORD15_OUT : WORD;
    _TMP_BYTE_2_WORD19_OUT : WORD;
    _TMP_BYTE_2_WORD25_OUT : WORD;
    _TMP_BITS_WORD23_OUT : WORD;
    _TMP_BYTE_2_WORD29_OUT : WORD;
    _TMP_INT_TO_WORD58_OUT : WORD;
    _TMP_BYTE_2_WORD60_OUT : WORD;
    _TMP_OR172_OUT : BOOL;
    _TMP_REAL_TO_TIME164_OUT : TIME;
    _TMP_MUL123_ENO : BOOL;
    _TMP_MUL123_OUT : REAL;
    _TMP_REAL_TO_INT125_ENO : BOOL;
    _TMP_REAL_TO_INT125_OUT : INT;
    _TMP_REAL_TO_TIME161_OUT : TIME;
    _TMP_BITS_BYTE78_OUT : BYTE;
    _TMP_BYTE_TO_BOOL77_OUT : BOOL;
    _TMP_BITS_BYTE50_OUT : BYTE;
    _TMP_BYTE_TO_BOOL49_OUT : BOOL;
    _TMP_MOD56_OUT : INT;
    _TMP_DIV63_OUT : INT;
    _TMP_INT_TO_BYTE65_OUT : BYTE;
  END_VAR

  stepper1(AXIS := AXIS2, TARGET := M2_POS_SET, RUN := M2_RUN, SPEED := M2_SPEED, ASPEED := M2_ASPEED, HOME := HOME2, HOMESPEED := HOME_SPEED2, HOMELOOKUP := HOME_SPEED_BACK2, XIFEN := XIFEN2, RUN_DIR := DIRC_N, HOMEMODE := HOME_P);
  M2_POS := stepper1.POS;
  TON1_Q := TON1.Q;
  TON2_Q := TON2.Q;
  stepper2(AXIS := AXIS3, TARGET := M3_POS_SET, RUN := TON1_Q, SPEED := M3_SPEED, ASPEED := M3_ASPEED, HOME := TON2_Q, HOMESPEED := HOME_SPEED3, HOMELOOKUP := HOME_SPEED_BACK3, XIFEN := XIFEN3, RUN_DIR := DIRC_P, HOMEMODE := HOME_P);
  M3_POS := stepper2.POS;
  RS0(S := _TMP_OR172_OUT, R1 := F_TRIG0.Q);
  Y1 := RS0.Q1;
  stepper0(AXIS := AXIS1, TARGET := M1_POS_SET, RUN := M1_RUN, SPEED := M1_SPEED, ASPEED := M1_ASPEED, HOME := HOME1, HOMESPEED := HOME_SPEED1, HOMELOOKUP := HOME_SPEED_BACK1, XIFEN := XIFEN1, RUN_DIR := DIRC_P, HOMEMODE := HOME_N);
  M1_POS := stepper0.POS;
  M2_STATUS := stepper1.STATE;
  M1_STATUS := stepper0.STATE;
  M3_STATUS := stepper2.STATE;
  HOME2 := stepper1.HOME;
  HOME1 := stepper0.HOME;
  stepper4(AXIS := AXIS5, TARGET := M7_POS_SET, RUN := M7_RUN, SPEED := M7_SPEED, ASPEED := M7_ASPEED, HOME := HOME7, HOMESPEED := HOME_SPEED7, HOMELOOKUP := HOME_SPEED_BACK7, XIFEN := XIFEN7, RUN_DIR := DIRC_P, HOMEMODE := HOME_P);
  M7_POS := stepper4.POS;
  stepper3(AXIS := AXIS4, TARGET := M6_POS_SET, RUN := M6_RUN, SPEED := M6_SPEED, ASPEED := M6_ASPEED, HOME := HOME6, HOMESPEED := HOME_SPEED6, HOMELOOKUP := HOME_SPEED_BACK6, XIFEN := XIFEN6, RUN_DIR := DIRC_N, HOMEMODE := HOME_N);
  M6_POS := stepper3.POS;
  M7_STATUS := stepper4.STATE;
  M6_STATUS := stepper3.STATE;
  HOME7 := stepper4.HOME;
  HOME6 := stepper3.HOME;
  _TMP_ADD165_OUT := ADD(EN := _TMP_REAL_TO_INT125_ENO, IN1 := _TMP_REAL_TO_INT125_OUT, IN2 := M9_POS, ENO => _TMP_ADD165_ENO);
  IF _TMP_ADD165_ENO THEN
      M9_POS := _TMP_ADD165_OUT;
  END_IF;
  _TMP_ADD126_OUT := ADD(EN := R_TRIG0.Q, IN1 := liandong_POS, IN2 := M7_POS_SET, ENO => _TMP_ADD126_ENO);
  IF _TMP_ADD126_ENO THEN
      M7_POS_SET := _TMP_ADD126_OUT;
  END_IF;
  _TMP_MOVE128_OUT := MOVE(EN := R_TRIG0.Q, IN := 1, ENO => _TMP_MOVE128_ENO);
  IF _TMP_MOVE128_ENO THEN
      M9_RUN := _TMP_MOVE128_OUT;
  END_IF;
  _TMP_MOVE168_OUT := MOVE(EN := TON0.Q, IN := 1, ENO => _TMP_MOVE168_ENO);
  IF _TMP_MOVE168_ENO THEN
      M7_RUN := _TMP_MOVE168_OUT;
  END_IF;
  _TMP_BYTE_2_WORD43_OUT := BYTE_2_WORD(M4_POS, M4_SPEED);
  MB3_HR_1002 := _TMP_BYTE_2_WORD43_OUT;
  _TMP_BYTE_2_WORD47_OUT := BYTE_2_WORD(_TMP_BITS_BYTE78_OUT, M4_MODE);
  MB3_HR_1000 := _TMP_BYTE_2_WORD47_OUT;
  _TMP_BYTE_2_WORD67_OUT := BYTE_2_WORD(M4_RUN, M4_FORCE);
  MB3_HR_1003 := _TMP_BYTE_2_WORD67_OUT;
  M4_STATUS := MB3_HR_2000;
  _TMP_BYTE_2_WORD15_OUT := BYTE_2_WORD(M8_POS, M8_SPEED);
  MB_HR_1002 := _TMP_BYTE_2_WORD15_OUT;
  _TMP_BYTE_2_WORD19_OUT := BYTE_2_WORD(_TMP_BITS_BYTE50_OUT, M8_MODE);
  MB_HR_1000 := _TMP_BYTE_2_WORD19_OUT;
  _TMP_BYTE_2_WORD25_OUT := BYTE_2_WORD(M8_RUN, M8_FORCE);
  MB_HR_1003 := _TMP_BYTE_2_WORD25_OUT;
  M8_STATUS := MB_HR_2000;
  _TMP_BITS_WORD23_OUT := BITS_WORD(M9_ACT);
  MB_HR_1001 := _TMP_BITS_WORD23_OUT;
  _TMP_BYTE_2_WORD29_OUT := BYTE_2_WORD(M9_SPEED, M9_FORCE);
  MB_HR_1005 := _TMP_BYTE_2_WORD29_OUT;
  _TMP_INT_TO_WORD58_OUT := INT_TO_WORD(_TMP_MOD56_OUT);
  MB_HR_1006 := _TMP_INT_TO_WORD58_OUT;
  MB_HR_1004 := _TMP_INT_TO_WORD58_OUT;
  _TMP_BYTE_2_WORD60_OUT := BYTE_2_WORD(M9_RUN, _TMP_INT_TO_BYTE65_OUT);
  MB_HR_1007 := _TMP_BYTE_2_WORD60_OUT;
  M9_STATUS := MB_HR_2001;
  R_TRIG1(CLK := M3_RUN);
  _TMP_OR172_OUT := OR(R_TRIG1.Q, R_TRIG2.Q);
  R_TRIG2(CLK := HOME3);
  TON1(IN := M3_RUN, PT := _TMP_REAL_TO_TIME164_OUT);
  F_TRIG0(CLK := stepper2.RUNING);
  _TMP_REAL_TO_TIME164_OUT := REAL_TO_TIME(M3_Delay);
  TON2(IN := HOME3, PT := _TMP_REAL_TO_TIME164_OUT);
  R_TRIG0(CLK := liandong);
  _TMP_MUL123_OUT := MUL(EN := R_TRIG0.Q, IN1 := liandong_POS, IN2 := M9_SCALE, ENO => _TMP_MUL123_ENO);
  _TMP_REAL_TO_INT125_OUT := REAL_TO_INT(EN := _TMP_MUL123_ENO, IN := _TMP_MUL123_OUT, ENO => _TMP_REAL_TO_INT125_ENO);
  TON0(IN := liandong, PT := _TMP_REAL_TO_TIME161_OUT);
  _TMP_REAL_TO_TIME161_OUT := REAL_TO_TIME(M7_DELAY);
  _TMP_BITS_BYTE78_OUT := BITS_BYTE(M4_ACT, 1, 0, _TMP_BYTE_TO_BOOL77_OUT, 0, 0, 0, 0);
  _TMP_BYTE_TO_BOOL77_OUT := BYTE_TO_BOOL(M4_RUN);
  _TMP_BITS_BYTE50_OUT := BITS_BYTE(M8_ACT, 1, 0, _TMP_BYTE_TO_BOOL49_OUT, 0, 0, 0, 0);
  _TMP_BYTE_TO_BOOL49_OUT := BYTE_TO_BOOL(M8_RUN);
  _TMP_MOD56_OUT := MOD(M9_POS, 360);
  _TMP_DIV63_OUT := DIV(M9_POS, 360);
  _TMP_INT_TO_BYTE65_OUT := INT_TO_BYTE(_TMP_DIV63_OUT);
END_PROGRAM


CONFIGURATION config

  RESOURCE resource1 ON PLC
    TASK task0(INTERVAL := T#10ms,PRIORITY := 0);
    PROGRAM instance0 WITH task0 : program0;
  END_RESOURCE
END_CONFIGURATION
